import { ILoadMapTypeAll } from '@/common';
import { getBaseUrl } from '@/services/urlconst';
import { getLevelFirst, getLevelZero, getTenantID, ntkoPageUrlParams, wordHandChildParam } from '@/utils';

/**
 * 合同范本管理
 * @param curRes
 * @returns
 */
const getManageTabSwitch = (curRes: Record<string, any>) => {
  if (curRes?.['/contractTemplateController/all']) {
    return {
      params: 'all',
    };
  } else if (curRes?.['/contractTemplateController/createCompanyID']) {
    return {
      createCompanyID: getLevelZero(),
      params: 'createCompanyID',
    };
  } else if (curRes?.['/contractTemplateController/createDepartmentID']) {
    return {
      createDepartmentID: getLevelFirst(),
      params: 'createDepartmentID',
    };
  }
  return {};
};

//TODO 范本管理
// /**
//  * 合同范本管理
//  * @param curRes
//  * @returns
//  */
// const getManageTabSwitch = (curRes: Record<string, any>) => {
//   if (curRes?.['/contractTemplateController/ownCompanyID']) {
//     return {
//       ownCompanyID: getLevelZero(),
//       params: 'ownCompanyID',
//     };
//   } else if (curRes?.['/contractTemplateController/ownDepartmentID']) {
//     return {
//       ownDepartmentID: getLevelFirst(),
//       params: 'ownDepartmentID',
//     };
//   }
//   return {};
// };

/** 在线编辑权限路劲处理 */
const ntkoQueryParams = (resObj: Record<string, any>, curRes: Record<string, any>, fieldName: string) => {
  let wordInfo: Record<string, any> = {
    filedUrl: resObj?.[fieldName],
    ntkoWordUrl: '',
  };
  let wordBtnQuery: Record<string, any> = {
    wordAllBtn: false, //全部按钮
    saveBtn: false, //保存按钮
    replaceBtn: false, //替换域按钮
  };
  if (
    curRes['/contractTemplateController/readUpdate'] &&
    (resObj?.state == `${getTenantID()}${ILoadMapTypeAll.CONTRACTTEMP_STATE}1000` ||
      resObj?.state == `${getTenantID()}${ILoadMapTypeAll.CONTRACTTEMP_STATE}1001`)
  ) {
    wordBtnQuery['saveBtn'] = true;
    wordBtnQuery['wordAllBtn'] = true;
  } else if (curRes['/contractTemplateController/readOnly']) {
    wordBtnQuery['wordAllBtn'] = false;
  }

  const wordQuery: string = ntkoPageUrlParams(wordBtnQuery);
  wordInfo['ntkoWordUrl'] = `${getBaseUrl()}/static/NtkoPlugin/index.html?cmd=1&isReadOnly=${wordQuery}&filedUrl=${getBaseUrl()}${wordInfo?.filedUrl}`;

  // wordInfo['ntkoWordUrl'] = `http://127.0.0.1:5500/index.html?cmd=1&isReadOnly=${wordQuery}&filedUrl=${getBaseUrl()}${wordInfo?.filedUrl}`;

  return {
    ...wordInfo,
    ntkoWordUrl: wordInfo?.ntkoWordUrl,
    filedUrl: wordInfo?.filedUrl,
    btnQuery: wordInfo,
  };
};

export { getManageTabSwitch, ntkoQueryParams };
